import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';

class CashFlowFilterController extends GetxController {
  DateType dateType = DateType.day;
  String startDayDate = "";
  String endDayDate = "";
  String startMonthDate = "";
  String endMonthDate = "";

  List<AccountType> accountTypeList = [];
  List<TransactionType> transactionTypeList = [];

  @override
  void onReady() {
    super.onReady();
    initList();
  }

  void reset() {
    dateType = DateType.day;
    startDayDate = "";
    endDayDate = "";
    startMonthDate = "";
    endMonthDate = "";
    if (accountTypeList.isNotEmpty) {
      accountTypeList.forEach((element) {
        element.isSelect = false;
      });
    }
    if (transactionTypeList.isNotEmpty) {
      transactionTypeList.forEach((element) {
        element.isSelect = false;
      });
    }
    update();
  }

  void setSelectAccountType(AccountType accountType) {
    accountTypeList.forEach((element) {
      if (element.name == accountType.name) {
        element.isSelect = !accountType.isSelect;
      } else {
        element.isSelect = false;
      }
    });
    update();
  }

  void setSelectTransactionType(TransactionType transactionType) {
    transactionTypeList.forEach((element) {
      if (element.name == transactionType.name) {
        element.isSelect = !transactionType.isSelect;
      } else {
        element.isSelect = false;
      }
    });
    update();
  }

  String getSelectAccountType() {
    String type = "";
    if (accountTypeList.isNotEmpty) {
      accountTypeList.forEach((element) {
        if (element.isSelect) {
          type = element.type;
        }
      });
    }
    return type;
  }

  String getSelectTransactionType() {
    String type = "";
    if (transactionTypeList.isNotEmpty) {
      transactionTypeList.forEach((element) {
        if (element.isSelect) {
          type = element.type;
        }
      });
    }
    return type;
  }

  Future<void> initList() async {
    bool isAdmin = await SPUtils.getBool(SPUtils.IS_ADMIN);
    if (isAdmin) {
      accountTypeList.add(AccountType("全部", ""));
      accountTypeList.add(AccountType("公司账户", Constant.COMPANY));
      accountTypeList.add(AccountType("车队账户", Constant.MOTORCADE));
      accountTypeList.add(AccountType("卡账户", Constant.CARD));
    } else {
      accountTypeList.add(AccountType("车队账户", Constant.MOTORCADE));
    }
    transactionTypeList.add(TransactionType("全部", ""));
    transactionTypeList.add(TransactionType("公司付款", Constant.COMPANY_SETTLEMENT_OUT));
    transactionTypeList.add(TransactionType("公司收款", Constant.COMPANY_SETTLEMENT_IN));
    transactionTypeList.add(TransactionType("金额拨入", Constant.ROLL_IN));
    transactionTypeList.add(TransactionType("金额拨出", Constant.ROLL_OUT));
    transactionTypeList.add(TransactionType("订单支付", Constant.ORDER_PAYMENT));
    transactionTypeList.add(TransactionType("订单作废", Constant.OTHER));
    transactionTypeList.add(TransactionType("充值", Constant.ACCOUNT_RECHARGE));
  }
}

enum DateType {
  day,
  month,
}

class AccountType {
  String name;
  String type;
  bool isSelect;

  AccountType(this.name, this.type, {this.isSelect = false});
}

class TransactionType {
  String name;
  String type;
  bool isSelect;

  TransactionType(this.name, this.type, {this.isSelect = false});
}
